package com.liveinstars.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * 封装fastjson功能（它的全局配置影响较大，弃用了）
 * @author guoyanjiang
 * @date 2019-09-09
 */
public class JsonUtil {

    private static final SerializerFeature[] DEFAULT_SERIALIZER_FEATURE = new SerializerFeature[]{
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteNullNumberAsZero,
            SerializerFeature.WriteNullStringAsEmpty,
            SerializerFeature.DisableCircularReferenceDetect
    };

    public static String toJson(Object o) {
        return JSON.toJSONString(o, DEFAULT_SERIALIZER_FEATURE);
    }

    public static byte[] toBytes(Object o) {
        return JSON.toJSONBytes(o, DEFAULT_SERIALIZER_FEATURE);
    }
}
